<?php
//部件

namespace app\admin\controller;


use app\admin\model\ComponentExceptionInfoRe;
use app\admin\model\ComponentPurchased;
use app\admin\model\CompReCompPurch;
use app\admin\model\compReCompProce;


class Component extends Base
{
    protected $middleware = ['app\admin\middleware\TableFields::class'];  //获取表结构，添加在返回数据中
    public $index_not_display_fields = 'id,update_time,projects_order_id,pid,exception_type';  //字段不会在后台列表页，字段选择中显示
    protected $lists_fields = 'id,component_type,drawing_no,drawing_name,number,material,weight,colour,processes_type,drawing_img,notes,ask,status,component_status,projects_order_id,pid,exception_type,create_time';

    public function filter(&$map, &$field)
    {
        $field = $this->lists_fields;
        //回收站
        $status = request()->get('status', 0);
        if($status == 3)
        {
            $map = [['status', '=', 3]];
        }else{
            $map = [['status', 'in', '1,2']];
        }

        //部件状态
        $component_status = input('component_status', 0);
        if(!empty($component_status))
        {
            $map = [['component_status', '=', $component_status]];
        }

        //图名
        $drawing_no = input('drawing_no');
        if(!empty($drawing_no)){
            $map[] = ['drawing_no', 'like', '%'.$drawing_no.'%'];
        }

        //图号
        $drawing_name = input('drawing_name');
        if(!empty($drawing_name)){
            $map[] = ['drawing_name', 'like', '%'.$drawing_name.'%'];
        }

        //创建时间时间
        $start_time = input('start_time', 0);
        $end_time = input('end_time', 0);
        if (!empty($end_time) && !empty($start_time)) {
            $map[] = ['start_time', 'BETWEEN', [$start_time, $end_time]];
        } else if ($end_time) {
            $map[] = ['start_time', '<', $end_time];
        } else if ($start_time) {
            $map[] = ['start_time', '>', $start_time];
        }

        //父级id
        $pid = input('pid', 0);
        $map = [['pid', '=', $pid]];

    }

    public function attachedIndex($lists_result)
    {
        foreach($lists_result as $k=>$v)
        {
            //todo 二维码地址 暂时写为固定值
            $lists_result[$k]['qrcode_url'] = '测试链接';

            //部件中的外购件详情
            $lists_result[$k]['purchased_lists'] =(new CompReCompPurch)->componentInPurchased($v['id']);

            //处理图纸图片
            if(isset($v['drawing_img']))
            {
                $lists_result[$k]['full_drawing_img'] = return_upload_url($v['drawing_img']);
            }

            //当前部件的子级是否含有异常（重置或修复）信息
            $is_exception =  (new ComponentExceptionInfoRe)->isException($this->run_controller, $v['id']);
            $lists_result[$k]['is_exceprion'] = (bool)$is_exception;

            //当前部件是否含有异常（重置或修复）信息
            $is_exception =  (new ComponentExceptionInfoRe)->isSelfException($this->run_controller, $v['id']);
            $lists_result[$k]['is_self_exceprion'] = (bool)$is_exception;
        }

        return $lists_result;
    }
    public function finishInsert($data, $id)
    {
        //关联外购件，同时向仓储部门发送信息
        (new CompReCompPurch)->componentRePurchased($data['purchased'], $id);

        //关联部件类型
        (new compReCompProce)->ComponentReProcesses($data['processes_type'], $id);

    }

    public function finishUpdate($data)
    {
        //关联外购件，同时向仓储部门发送信息
        (new CompReCompPurch)->componentRePurchased($data['purchased'], $data['id'], 1);

        //关联部件类型
        (new compReCompProce)->componentReProcesses($data['processes_type'], $data['id'], 1);

    }


    public function attachedDetail(&$data)
    {
        $data['purchased_lists'] =(new CompReCompPurch)->componentInPurchased($data['id']);    //部件中的外购件详情
    }

    public function finishDel($ids, $is_del)
    {
        //删除关联的外购件
        CompReCompPurch::where('component_id', 'in', $ids)->delete();
        //删除关联的部件类型
        compReCompProce::where('component_id', 'in', $ids)->delete();
        //发送信息通知仓储部门，订单被取消
        foreach($ids as $k=>$v)
        {
            $content = (new ComponentPurchased)->getToWarehouseDelPurchNoticeContent($v);
            if(!empty($content))
            {
                $department_ids = get_config_cache('department_ids');
                $warehouse_dep_id = $department_ids['warehouse_dep_id']; //仓储部门
                $notice_data = [
                    'tmp_flag' => 'system', //消息模板标识
                    'to_dep_id' => $warehouse_dep_id, //发送的部门id
                    'content' => [
                        'data' => $content
                    ]
                ];
                event('SendNotice', $notice_data);
            }
        }
    }

    public function beforeInsert(&$data)
    {
        $data['notes'] = empty($data['notes']) ? '' : $data['notes'];
        $data['ask'] = empty($data['ask']) ? '' : $data['ask'];
    }

    public function beforeUpdate(&$data)
    {
        $data['notes'] = empty($data['notes']) ? '' : $data['notes'];
        $data['ask'] = empty($data['ask']) ? '' : $data['ask'];
    }
}